<?php
declare (strict_types=1);

namespace app\index\middleware;

class Template
{
    /**
     * 处理请求
     *
     * @param $request
     * @param \Closure $next
     * @return mixed
     */
    public function handle($request, \Closure $next)
    {
        /**
         * 处理前端模板
         */
        if (saenv('site_state') && saenv('site_type')) {
            $hosts = request()->header()['host'];
            $mobile = str_replace(['https://', 'http://', '://'], '', (string)saenv('site_mobile'));
            if ((request()->isMobile() && !$mobile) || ($mobile && $mobile == $hosts)) {
                $template = root_path('app/mobile/view');
                app()->view->config(['view_path' => $template]);
            }
        }

        return $next($request);
    }
}
